package com.sis.main.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sis.main.client.intf.IntfSecuritiesPositionService;
import com.sis.main.client.intf.IntfSecuritiesPositionServiceAsync;

/**
 * Called by cwViewSecurities.onInitialize
*/
public class PopupLogin extends PopupPanel {
	  static boolean autoHide = false;
	  static boolean modal = true;
	  final PopupLogin popuplogin = this;
	  private VerticalPanel vpanelLogon = new VerticalPanel();
	  private Label labelUsername = new Label("Username: ");
	  private TextBox textboxUsername = new TextBox();
	  private Label labelPassword = new Label("Password: ");
	  private PasswordTextBox passwordtextboxPassword = new PasswordTextBox();
	  private Button buttonSubmit = new Button("Submit");
	  private DataLogin dataLogin = new DataLogin();
	  
	    public PopupLogin(final Widget cwViewSecurities) {
	      super (autoHide, modal);
	      vpanelLogon.add(labelUsername);
	      vpanelLogon.add(textboxUsername);
	      vpanelLogon.add(labelPassword);
	      vpanelLogon.add(passwordtextboxPassword);
	      
	      // Add a normal button
	      buttonSubmit.addClickHandler( new ClickHandler() {
	            public void onClick(ClickEvent event) {
	              dataLogin.setUserID(textboxUsername.getValue());
	              dataLogin.setPassword(passwordtextboxPassword.getValue());
	              dataLogin.setLoggedIn(false);
	              
	      	    // Check login status using login service.
	      	    IntfSecuritiesPositionServiceAsync loginService = GWT.create(IntfSecuritiesPositionService.class);
	      	    loginService.getDataLogin(dataLogin, new AsyncCallback<DataLogin>() {
	      	      public void onFailure(Throwable error) {
	      	      }

	      	      public void onSuccess(DataLogin dataLogin) {  
	      	        if( ! dataLogin.isLoggedIn()) {
	      	          Window.alert("Login Failed");	      	       
	      	        }else{
	      	        	setDataLogin (dataLogin);
	      	        	cwViewSecurities.setVisible(true);
	      	        	popuplogin.hide();
	
	      	        }
	      	      }
	      	    });//end Asyncallback

	            }
	          });//end onClick
	      vpanelLogon.add(buttonSubmit);


	      // PopupPanel is a SimplePanel, so you have to set it's 
	      //widget property to
	      // whatever you want its contents to be.
	      setWidget(vpanelLogon);
	      this.center();
	      this.show();
	    }
	    
	    public DataLogin getDataLogin() {
	    	return dataLogin;
	    }
	    
	    public void setDataLogin (DataLogin dataLogin) {
	    	this.dataLogin = dataLogin;
	    }
	    
	    
	  }// end class

